<?php

class edit_team
{
	var $mess;

	function edit_team()
	{
		if(isset($_POST['removeItem']) && isset($_POST['itemID']))
		{
			$this->doDelete();
		}
		else if(isset($_POST['addRecord']) && isset($_POST['id']))
		{
			$this->doAdd();
		}
		else if(isset($_POST['editRecord']) && isset($_POST['id']))
		{
			$this->doEdit();
		}
	}

	function render()
	{		
		include_once('includes/addMessage.php');
		
		if(isset($_POST['editItem']) && isset($_POST['itemID']))
		{
			$this->renderEdit();
		}
		else if(isset($_POST['removeItem']) && isset($_POST['itemID']))
		{
			$this->mess->render();
			$this->renderAdd();
		}
		else if(isset($_POST['addRecord']) && isset($_POST['id']))
		{
			$this->mess->render();
			$this->renderAdd();
		}
		else if(isset($_POST['editRecord']) && isset($_POST['id']))
		{
			$this->mess->render();
			$this->renderAdd();
		}
		else
		{
			$this->renderAdd();
		}
	}
	
	function doDelete()
	{
		global $skin_dir, $db, $lang;
		
		//get the record
		$qSel = "select t_name from ". TEAMS_TABLE . " Where t_id = " . $_POST['itemID'];
		$result = $db->sql_query($qSel);
		if($row = $db->sql_fetchrow($result)) 
		{
			$qDel = "delete from ". TEAMS_TABLE . " Where t_id = " . $_POST['itemID'];
			$result = $db->sql_query($qDel);
			$this->mess = new addMessage(str_replace('{entry}', $row['t_name'], $lang['AdminRemoved']), $lang['AdminRemovedTitle']);
			
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			
		}
	}
	
	function doAdd()
	{
		global $db, $lang;
		
		if($this->validateFields(false)) 
		{
			$qUpd = "insert into  ". TEAMS_TABLE . " (t_name, t_nationality, t_website, t_description, t_parent) values('" . 
							$_POST['teamname'] . "', '" . $_POST['nationality'] . "', '" . $_POST['website'] . "', '" . 
							$_POST['description'] . "', " . $_POST['parent'] . ")" ;
			$result = $db->sql_query($qUpd);
			$this->mess = new addMessage(str_replace('{entry}', $_POST['teamname'], $lang['AdminAdded']), $lang['AdminAddedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function doEdit()
	{
		global $db, $lang;
		
		if($this->validateFields(true)) 
		{
			$qUpd = "update  ". TEAMS_TABLE . " set t_name = '" . $_POST['teamname'] .
													  "', t_nationality = '" . $_POST['nationality'] .
													  "', t_website= '" . $_POST['website'] .
													  "', t_description= '" . $_POST['description'] .
													  "', t_parent= " . $_POST['parent'] . 
													  " WHERE t_id = " . $_POST['id'] ;
			$result = $db->sql_query($qUpd);
			
			$this->mess = new addMessage(str_replace('{entry}', $_POST['teamname'], $lang['AdminUpdated']), $lang['AdminUpdatedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function validateFields($checkID = false)
	{
		$valid = true;
		
		if($checkID)
		{
			$valid = is_numeric($_POST['id']);
		}
		$valid = $valid && is_numeric($_POST['parent']);
		
		return $valid;
					  
	}
	
	function renderEdit()
	{
		global $skin_dir, $db, $lang;
		
		//get the record
		$q = "Select * From ". TEAMS_TABLE . " Where t_id = " . $_POST['itemID'];
		$result = $db->sql_query($q);
		if($row = $db->sql_fetchrow($result)) 
		{
			$parentOptions = '<option value="0" ' . ($row['t_parent'] == 0 ? 'SELECTED' : '') . 
							 '>' . $lang['TeamParent'] . '</option>';
			$q = "Select t_name, t_id FROM " . TEAMS_TABLE . " 
				  Where t_parent = 0 AND t_id != " . $_POST['itemID'] . '
				  ORDER BY t_name ASC';
			$result = $db->sql_query($q);
			while($parent = $db->sql_fetchrow($result)) 
			{
				$parentOptions .= '<option value="' . $parent['t_id'] . '" ' . 
								  ($row['t_parent'] == $parent['t_id']  ? 'SELECTED' : '') . 
							 	  '>' . $parent['t_name'] . '</option>';
			}
		
			$mess = new addMessage($lang['AdminEdit'], $lang['AdminEditTitle']);
			$mess->render();
			
			$submitValue = 'editRecord';
			$submitText = $lang['SaveButton'];
					
			include $skin_dir . 'admin/editteam.tpl';
		}
		else
		{
			$mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			$mess->render();
		}
	}

	function renderAdd()
	{
		global $skin_dir,  $lang, $db;
		
		$parentOptions = '<option value="0">' . $lang['TeamParent'] . '</option>';
		$q = "Select t_name, t_id FROM " . TEAMS_TABLE . " 
			  Where t_parent = 0 
			  ORDER BY t_name ASC";
		$result = $db->sql_query($q);
		while($parent = $db->sql_fetchrow($result)) 
		{
			$parentOptions .= '<option value="' . $parent['t_id'] . '">' . $parent['t_name'] . '</option>';
		}
			
		$mess = new addMessage($lang['AdminAdd'], $lang['AdminAddTitle']);
		$mess->render();
		
		$row['t_id'] = '';
		$row['t_name'] = '';
		$row['t_website'] = '';
		$row['t_nationality'] = '';
		$row['t_description'] = '';
		$row['t_logo'] = '';
		
		$submitValue = 'addRecord';
		$submitText = $lang['AddButton'];
		
		include $skin_dir . 'admin/editteam.tpl';
	}
	
}
?>